<?php


namespace Api\Controller;


/** 
 * 点赞 
 function:

 	add    		添加点赞   
 	cancel  	取消点赞
 	lists       点赞列表
 
 * @author Legend. <xcx_legender@qq.com>
 * @param 
 * @return 
 */

class LikeController extends UserBaseController 
{


	public $id;
	public $type;

	/**
	*开放操作，非登录下
	*/
	static $openAction = array('lists');

	/**
	*参数初始化
	*@author susy.liang <liangling@ilongyuan.com.cn>
	*/
	protected function _initialize()
	{
		parent::_initialize();
	}

	/** 
	 * 点赞
	 * @author Legend. <xcx_legender@qq.com>
	 * @param  $id; $type;
	 * @return 
	 */
	
	public function add()
	{
		$this->checkAuth('zan');
		$this->check_param();
		$lEvent = A('Like', 'Event');
		$data = [];
		$res = $lEvent->add($this->type, UID, $this->id, $data);
		if (!$res)
		{
			$this->error(self::$API_ERROR['HAS_LIKE'], '已经点赞');
		}
		$data['total'] = $this->getStatis();
		$this->success('操作成功', $data);
	}

	// 取消
	public function cancel()
	{
		$this->check_param();
		$lEvent = A('Like', 'Event');
		$res = $lEvent->cancel($this->type, UID, $this->id);
		if (!$res)
		{
			$this->error(self::$API_ERROR['HAS_NO_LIKE'], '未点赞不能取消');
		}
		$data['total'] = $this->getStatis();
		$this->success('操作成功', $data);
	}

	// 检查参数
	protected function check_param()
	{
		$id   = I('id', 0, 'intval');
		$type = I('type', 0, 'intval');
		$this->type = intval($type) ? $type : 0;
		

		switch ($this->type) {
			case 1:
				if (!$id)
				{
					$this->param_error();
				}
				break;
			case 0:
				if (!$id)
				{
					$this->param_error();
				}
				$vEvent = A('Video', 'Event');
				$video = $vEvent->info( $id );
				if (!$video)
				{
					$this->error(self::$API_ERROR['VIDEO'], '视频信息错误');
				}
				break;
			case 2:
				if (!I('nickname')){
					$this->param_error();
				}
				$id = event('User')->getUser(I('nickname'));
				if ( $id == UID) {
					$this->error( self::$API_ERROR['USER'], '不能对自己点赞' );
				}
				if (!$id){
					$this->error(self::$API_ERROR['USER'], '用户信息错误');
				}
				break;
			case 3:
				if (!$id) {
					$this->param_error();
				}
				$comment = service('Comments')->where(['id' => (int)$id])->find();
				if (!$comment) {
					$this->error(self::$API_ERROR['COMMENT'], '评论信息错误');
				}
				break;
		}

		$this->id = $id;
	}

	// 获取点赞总数
	protected function getStatis()
	{
		switch ($this->type) {
			case 1:

				break;
			case 2:
				$statis = event('User')->getLikeCount($this->id);	
				return $statis;
			case 0:
				$statis = event('Video')->getStatis( $this->id );
				return $statis ? $statis['zan'] : 0;
				break;
			case 3:
				$statis = event('Comments')->getLikeCount($this->id);
				return intval($statis);
		}
	}

	/**
	*获取视频或赛事点赞用户的列表信息
	*根据p分页获取目标id的信息
	*@author susy.liang<liangling@iliongyuan.com.cn>
	*/
	public function lists(){

		$id = I('id', 0, 'intval');

		$video = A('Api/Video', 'Event')->info($id);
		if(!$video)
			$this->error(self::$API_ERROR['VIDEO'], '视频信息错误');

		$data = array();
		$total = $video['zan'];
		if($total){
			$p = I('p', 1, 'intval');
			$pager 	   = I('pager', C('USER_LIST_NUM',NULL, 20), 'intval');
			$likers = A('Api/Like', 'Event')->getUser($id, $pager, $p);

			$data = array('list'=>$likers, 'total'=>$total);
		}

		$this->ajaxReturn(array('code'=>200, 'info'=>'操作成功', 'data'=>$data));
	}
	
}